//
//  DiaryHeaderView.m
//  MyTrip
//
//  Created by zhang on 15/8/24.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "DiaryHeaderView.h"
#import "FigureCell.h"
#import "Desc.h"
#import "UIImageView+WebCache.h"
#define kCollectionSection 100
#define kWidth [UIScreen mainScreen].bounds.size.width

@interface DiaryHeaderView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIPageControl *pageControl;


@end

@implementation DiaryHeaderView

- (void)dealloc
{
    [_diaryHeaderArray release];
    [_diaryHeader release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.diaryHeaderArray = [NSMutableArray array];
        [self createDiaryHeader];
    }
    return self;
}


- (void)createDiaryHeader{
    
    UICollectionViewFlowLayout *flowLaout =[[UICollectionViewFlowLayout alloc] init];
    flowLaout.minimumInteritemSpacing = 0;
    flowLaout.minimumLineSpacing = 0;
    flowLaout.itemSize = CGSizeMake(self.frame.size.width, 200 * OffHeight);
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.diaryHeader = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200 * OffHeight) collectionViewLayout:flowLaout];
    _diaryHeader.backgroundColor = [UIColor whiteColor];
    _diaryHeader.delegate = self;
    _diaryHeader.dataSource = self;
    [_diaryHeader registerClass:[FigureCell class] forCellWithReuseIdentifier:@"diaryHeader"];
    _diaryHeader.pagingEnabled = YES;
    [self addSubview:_diaryHeader];
    [flowLaout release];
    [_diaryHeader release];
    
}

- (void)createPage{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 2, 30 * OffHeight)];
    self.pageControl.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20 * OffHeight);
    //    [self.pageControl setBackgroundColor:[UIColor blackColor]];
    self.pageControl.numberOfPages = _diaryHeaderArray.count;
    [self addSubview:self.pageControl];
    [_pageControl release];
    [self addTimer];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _diaryHeaderArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Desc *desc = [_diaryHeaderArray objectAtIndex:indexPath.item];
    FigureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"diaryHeader" forIndexPath:indexPath];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:desc.image_url]];
    return cell;
}


- (void)setImageArr:(NSMutableArray *)imageArr
{
    _diaryHeaderArray = imageArr;
    self.pageControl.numberOfPages = _diaryHeaderArray.count;
    
    [self.diaryHeader scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:kCollectionSection / 2] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
}


- (void)addTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(scrollNext) userInfo:nil repeats:YES];
    // NSRunLoop持有NSTimer, 保证安全
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    self.timer = timer;
}

- (void)removeTimer
{
    // 将NSTimer释放(release操作)
    [self.timer invalidate];
    // 置空
    self.timer = nil;
}

- (NSIndexPath *)returnIndexPath
{
    NSIndexPath *currentIndexPath = [[self.diaryHeader indexPathsForVisibleItems] lastObject];
    currentIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row inSection:kCollectionSection / 2];
    [self.diaryHeader scrollToItemAtIndexPath:currentIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return currentIndexPath;
}

- (void)scrollNext
{
    NSIndexPath *indexPath = [self returnIndexPath];
    NSInteger item = indexPath.row + 1;
    NSInteger section = indexPath.section;
    if (item == _diaryHeaderArray.count) {
        item = 0;
        section++;
    }
    self.pageControl.currentPage = item;
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:item inSection:section];
    [self.diaryHeader scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x / (kWidth - 20)) % self.diaryHeaderArray.count;
    self.pageControl.currentPage = page;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return kCollectionSection;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    html_url
     Desc *desc = [_diaryHeaderArray objectAtIndex:indexPath.item];
    [self.delegate clickIndex:indexPath WithURL:desc.html_url];
}



@end
